package com.bana.fvs.protocol;

import com.bana.fvs.model.Controller;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * 查询能见度等级参数
 */
public class QuerySolarEnergy {

    public int getId()
    {
        return 0x2f;
    }

    public byte[] toBuffer(int kzqbsm)
    {
        byte[] buffer=new byte[4];
        buffer[0]=(byte)0x81;
        buffer[1]=(byte)kzqbsm;
        buffer[2]=(byte)0x2f;
        buffer[3]=(byte)0xff;
        return buffer;
    }

    public Controller toModel(byte[] buffer) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        DataInputStream dis = new DataInputStream(bais);
        dis.skipBytes(1);
        Controller controller=new Controller();
        controller.setKzqbsm(dis.readByte());
        dis.skipBytes(1);
        controller.setHasSolarEnergyData(true);
        byte b4=dis.readByte();
        controller.setDcqygj((b4&0x01)>0);
        
        byte b5=dis.readByte();
        controller.setDcdl(b5);

        dis.skipBytes(1);

        byte b7=dis.readByte();
        byte b8=dis.readByte();
        controller.setDcdy(String.format("%.1f",(b8*128+b7)*0.1));

        byte b9=dis.readByte();
        byte b10=dis.readByte();
        controller.setTyndy(String.format("%.1f",(b10*128+b9)*0.1));

        byte b11=dis.readByte();
        byte b12=dis.readByte();
        controller.setCddl(String.format("%.2f",(b12*128+b11)*0.01));

        dis.close();
        bais.close();
        return controller;
    }
}
